using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class SpriteInterpret : MonoBehaviour
{
	int _FPS = 30;
	float _delta = 0f;
	int _index = 0;
	
	Dictionary<int,Image> imageDic = new Dictionary<int, Image> ();
	Dictionary<int,Module> moduleDic = new Dictionary<int, Module> ();
	Dictionary<int,FrameModule> frameModuleDic = new Dictionary<int, FrameModule> ();
	Dictionary<int ,Frame> frameDic = new Dictionary<int, Frame> ();
	
	Dictionary<string,ClipAnimation> animations = new Dictionary<string, ClipAnimation>();
	
	class Image
	{
		int _imageID;
		string _path;
		
		public Image (string imageInfoString)
		{
			string[] myStrings = imageInfoString.Split ('"');
			string[] stringID = myStrings [0].Split ('x');
			_imageID = Int32.Parse (stringID [1], System.Globalization.NumberStyles.HexNumber);
			_path = myStrings [1].Substring (1);
			Debug.Log (_path);
		}
	}
	
	class Module
	{
		int _moduleId;
		int _imageId;
		int _x;
		int _y;
		int _width;
		int _height;
		
		public Module (string moduleInfoString)
		{
			string[] myStrings = Regex.Split (moduleInfoString, @"\s+");
			string[] stringID = myStrings [1].Split ('x');
			_moduleId = Int32.Parse (stringID [1], System.Globalization.NumberStyles.HexNumber);
			_imageId = Convert.ToInt32 (myStrings [3]);
			_x = Convert.ToInt32 (myStrings [4]);
			_y = Convert.ToInt32 (myStrings [5]);
			_width = Convert.ToInt32 (myStrings [6]);
			_height = Convert.ToInt32 (myStrings [7]);
		}
		
		public int moduleId {
			get {
				return _moduleId;
			}
		}
		
		public int imageId {
			get {
				return _imageId;
			}
		}
		
		public int x {
			get {
				return _x;
			}
		}
		
		public int y {
			get {
				return _y;
			}
		}
	}
	
	class FrameModule
	{
		int _moduleID;
		int _ox;
		int _oy;
		
		public FrameModule (string frameModuleString)
		{
			string[] myStrings = Regex.Split (frameModuleString, @"\s+");
			string[] stringID = myStrings [1].Split ('x');
			_moduleID = Int32.Parse (stringID [1], System.Globalization.NumberStyles.HexNumber);
			_ox = Convert.ToInt32 (myStrings [2]);
			_oy = Convert.ToInt32 (myStrings [3]);
		}
	}
	
	class Frame
	{
		int _frameID;
		FrameModule[] _frameModule;
		
		public Frame (int frameId, FrameModule[] fms)
		{
			_frameID = frameId;
			_frameModule = fms;
		}
	}
	
	class Clip
	{
		int _frameID;
		int _time;
		int _ox;
		int _oy;
		
		public Clip (string clipString)
		{
			string[] myStrings = Regex.Split (clipString, @"\s+");
			string[] stringID = myStrings [1].Split ('x');
			_frameID = Int32.Parse (stringID [1], System.Globalization.NumberStyles.HexNumber);
			//TODO
			//frameID = Frame.GetFrameWithID (id);
			
			_time = Convert.ToInt32 (myStrings [2]);
			_ox = Convert.ToInt32 (myStrings [3]);
			_oy = Convert.ToInt32 (myStrings [4]);
		}
		
		public int frameID {
			get {
				return _frameID;
			}
		}
		
		public int time {
			get {
				return _time;
			}
		}
		
		public int ox {
			get {
				return _ox;
			}
		}
		
		public int oy {
			get {
				return _oy;
			}
		}
	}
	
	class ClipAnimation
	{
		int _animationID;
		string _animationName;
		Clip[] _AnimationClips;
		
		public int ID {
			get {
				return _animationID;
			}
			set {
				_animationID = value;
			}
		}
		
		public string Name {
			get {
				return _animationName;
			}
			set {
				_animationName = Name;
			}
		}
		
		public Clip this [int index] {
			get {
				return _AnimationClips [index];
			}
		}
		
		public int ClipsCount {
			get {
				return _AnimationClips.Length;
			}
		}
		
		public ClipAnimation (int id, string name, Clip[] clips)
		{
			_animationID = id;
			_animationName = name;
			_AnimationClips = clips;
		}
		
		public void Play ()
		{
			//show(_AnimationClips[]);
		}
		
		void show (Clip clip)
		{
			
		}
	}
	
	string filePath = "";
	Dictionary<int,string> helpInfoDic = new Dictionary<int, string> ();
	
	void Start ()
	{
		filePath = Application.dataPath + "/baize.sprite";
		
		Interpret();
		
		animations["daiji"].Play();
	}
	
	void Interpret ()
	{
		if (!File.Exists (filePath)) {
			Debug.Log ("file not exits");
		} else {
			string[] temp = File.ReadAllLines (filePath);
			
			StreamReader SR;
			string currentString;
			SR = File.OpenText (filePath);
			currentString = SR.ReadLine ();
			int key = 0;
			while (currentString != null) {
				currentString = currentString.Trim ();
				if (!currentString.StartsWith ("//")) {
					if (currentString.StartsWith ("IMAGE")) {
						int endIndex = currentString.LastIndexOf ('"');
						currentString = currentString.Substring (0, endIndex);
						Image image = new Image (currentString);
						//TODO
					} else if (currentString.StartsWith ("MD")) {
						Module module = new Module (currentString);
						
						moduleDic.Add (module.moduleId, module);
						//TODO
					} else if (currentString.StartsWith ("FRAME")) {
						SR.ReadLine ();
						string tempStr = SR.ReadLine ();
						tempStr = tempStr.Trim ();
						tempStr = tempStr.Split ('x') [1];
						//TODO
						int frameID = Int32.Parse (tempStr, System.Globalization.NumberStyles.HexNumber);
						
						tempStr = SR.ReadLine ();
						
						List<FrameModule> frameModuleList = new List<FrameModule> ();
						
						while (!tempStr.Contains("}")) {
							tempStr = tempStr.Trim ();
							FrameModule fm = new FrameModule (tempStr);
							frameModuleList.Add (fm);
							
							//TODO
							tempStr = SR.ReadLine ();
						}
						FrameModule[] fms = frameModuleList.ToArray ();
						Frame frame = new Frame (frameID, fms);
						frameDic.Add (frameID, frame);
					} else if (currentString.StartsWith ("ANIM")) {
						List<Clip> clipList = new List<Clip> ();
						string animationName = currentString.Split ('"') [1];
						SR.ReadLine ();
						string tempStr = SR.ReadLine ();
						tempStr = tempStr.Trim ();
						tempStr = tempStr.Split ('x') [1];
						//TODO
						int animationID = Int32.Parse (tempStr, System.Globalization.NumberStyles.HexNumber);
						
						tempStr = SR.ReadLine ();
						while (!tempStr.Contains("}")) {
							tempStr = tempStr.Trim ();
							Clip clip = new Clip (tempStr);
							clipList.Add (clip);
							
							tempStr = SR.ReadLine ();
						}
						Clip[] clips = clipList.ToArray ();
						ClipAnimation clipAnimation = new ClipAnimation (animationID, animationName, clips);
						animations.Add(clipAnimation.Name,clipAnimation);
					}
				} else {
					helpInfoDic.Add (key, currentString);
					key++;
				}
				
				currentString = SR.ReadLine ();
			}
			
			SR.Close ();
		}
	}
	
	void Update(){
		
	}
}
